package com.digitalchina.lxnsy.view.ChannelView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.RealTimeMarketV2Activity;
import com.digitalchina.lxnsy.api.BusinessRealTimePrice;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PriceTabV2VO;
import com.digitalchina.lxnsy.entity.TopSortVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.PriceTabV2Holder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：TabDemo
 * 类描述：
 * 包名：com.digitalchina.tabdemo
 * 创建人：郑皓
 * 创建时间：2017/7/17 21:24
 * 修改备注：
 * ================================================
 */
public class PriceFragmentV2 extends BaseV4Fragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private RecyclerArrayAdapter adapter;
    private List<PriceTabV2VO> priceTabVOList = new ArrayList<>();
    private List<TopSortVO> twoList = new ArrayList<>(); // 第2行分类(3级)
    private Map<String, Object> map = new HashMap<>();

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private String code = "";
    private String pCode = "";
    private String i = "";

    CallBackValue callBackValue;

    private int page = 1;
    private int limit = 20;


    @Override
    public View setRootView() {

        return inflate(R.layout.fragment_price);
    }

    /**
     * fragment与activity产生关联  回调这个方法
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue = (CallBackValue) getActivity();
    }


    @Override
    public void initMember() {
        if (getArguments() != null) {
            code = getArguments().getString("code");
            i = getArguments().getString("i");
            pCode = getArguments().getString("pCode");
            twoList = (List<TopSortVO>) getArguments().getSerializable("twoList");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new PriceTabV2Holder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1.0f, 10.0f, 10.0f));

        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                map.put("twoList", twoList);
                map.put("i", i);
                map.put("PriceTabV2VO", adapter.getItem(position));
                GotoUtil.gotoActivity(getActivity(), RealTimeMarketV2Activity.class, "map", map);
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (getActivity() == null || getActivity().isFinishing())
                    return;
                switch (msg.what) {
                    case MSG.MSG_GET_PRICE_V2_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PRICE_V2_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        priceTabVOList = FastJsonUtil.getListBean(object.toString(), "priceList", PriceTabV2VO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }


        adapter.addAll(priceTabVOList);

        if (priceTabVOList != null) {
            if (priceTabVOList.size() < limit) {
                adapter.stopMore();
            }
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessRealTimePrice.getProducePriceStatisticsListV2(getActivity(), pCode, code, String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void onLoadMore() {

        if (priceTabVOList != null && priceTabVOList.size() >= limit) {
            page++;
            BusinessRealTimePrice.getProducePriceStatisticsListV2(getActivity(), pCode, code, String.valueOf(page), String.valueOf(limit), mHandler);
        }
    }

    //定义一个回调接口
    public interface CallBackValue {
        public void SendMessageValue(String strValue);
    }

}